using UnityEngine;

namespace ZFramework.Runtime
{
    public class UIForm : MonoBehaviour, IUIForm
    {
        private int _serialId;
        private string _uiAssetPath;
        private UIGroup _uiGroup;
        private int _depthInUIGroup;
        private bool _pauseCoveredUIForm;
        private UIFormLogic _uiFormLogic;

        public int SerialId => _serialId;
        public string UIFormAssetName => _uiAssetPath;

        public Object Obj => gameObject;

        public bool PauseCoveredUIForm => _pauseCoveredUIForm;

        public IUIGroup UIGroup => _uiGroup;
        
        public int DepthInUIGroup => _depthInUIGroup;

        public void OnInit(int serialId, string uiAssetPath, UIGroup uiGroup, bool pauseCoveredUIForm,
            bool isNewInstance, object userData)
        {
            _serialId = serialId;
            _uiAssetPath = uiAssetPath;
            _uiGroup = uiGroup;
            _depthInUIGroup = 0;
            _pauseCoveredUIForm = pauseCoveredUIForm;

            if (!isNewInstance)
                return;

            _uiFormLogic = GetComponent<UIFormLogic>();
            if (_uiFormLogic == null)
            {
                Log.Error("UIForm path=={0} 没有UIFormLogic，请检查", uiAssetPath);
                return;
            }

            _uiFormLogic.OnInit(userData);
        }

        public void OnRecycle()
        {
            _uiFormLogic.OnRecycle();

            _serialId = 0;
            _depthInUIGroup = 0;
            _pauseCoveredUIForm = true;
        }

        public void OnOpen(object userData)
        {
            _uiFormLogic.OnOpen(userData);
        }

        public void OnClose(bool isShutdown, object userData)
        {
            _uiFormLogic.OnClose(userData);
        }

        //暂停
        public void OnPause()
        {
            _uiFormLogic.OnPause();
        }

        //暂停恢复
        public void OnResume()
        {
            _uiFormLogic.OnResume();
        }

        //覆盖
        public void OnCover()
        {
            _uiFormLogic.OnCover();
        }

        //覆盖恢复
        public void OnReveal()
        {
            _uiFormLogic.OnReveal();
        }

        //界面激活
        public void OnRefocus(object userData)
        {
            _uiFormLogic.OnRefocus(userData);
        }

        //轮询
        public void OnUpdate(float elapseSeconds, float realElapseSeconds)
        {
            _uiFormLogic.OnUpdate(elapseSeconds, realElapseSeconds);
        }

        //界面深度改变
        public void OnDepthChanged(int uiGroupDepth, int depthInUIGroup)
        {
            _depthInUIGroup = depthInUIGroup;
            _uiFormLogic.OnDepthChanged(uiGroupDepth, depthInUIGroup);
        }

    }
}
